package com.boot2523.control;

import com.boot2523.bean.Dept;
import com.boot2523.bean.Emp;
import com.boot2523.service.DeptS;
import com.boot2523.service.EmpS;
import com.google.gson.Gson;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "emp")
public class EmpC {
    @Resource
    public EmpS es;
    @PostMapping(value = "/insert")
    public int insert(Emp e, MultipartFile pic) throws IOException {
        String fname=pic.getOriginalFilename();
        if (fname!=null&&!fname.equals("")){
            e.setEpic(fname);
            e.setEpic2(pic.getInputStream());
        }
        return es.insert(e);

    }
    @GetMapping(value = "/all")
    public List<Emp> all(HttpServletRequest req){
        //获取服务器路径
        String path=req.getServletContext().getRealPath("upload");
        File f=new File(path);
        //创建目录
        if (!f.exists())
            f.mkdir();
        //将查询的所有转json串写入页面中
        List<Emp> data=es.all(path);
        return data;
    }
    @DeleteMapping(value = "/del")
    public int del(int id){
        return es.del(id);
    }
    @Resource
    public DeptS ds;
    @GetMapping(value = "/one")
    public Map<String,Object> one(int id){
        Emp one=es.one2(id);
        List<Dept> list=ds.all();
        Map<String,Object> data=new HashMap<>();
        //2.将数据装入map中
        data.put("one",one);
        data.put("list",list);
        return data;
    }
    @PostMapping(value = "/update")
    public int update(Emp e, MultipartFile pic) throws IOException {
        if (pic!=null) {
            String fname = pic.getOriginalFilename();
            if (fname != null && !fname.equals("")) {
                e.setEpic(fname);
                e.setEpic2(pic.getInputStream());
            }
        }
        return es.updateSelective(e);

    }

}
